<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Data_rekap extends CI_Controller
{
	function __construct(){
		parent::__construct();	
		$this->load->library('form_validation');			
		$this->load->model('helpdesk_model');		
	}
	
	function index()
	{		
		$this->data();
	}
	
	function menu_rekap()
	{
		$jenis_laporan_options['all']= '-- Pilih Tipe Rekap --';
		$jenis_laporan_options['0']= 'Excel';
		$jenis_laporan_options['1']= 'Grafik';

		$bulan_options['all'] = '-- Semua Bulan --';
		$bulan_options['1'] = 'Januari';
		$bulan_options['2'] = 'Februari';
		$bulan_options['3'] = 'Maret';
		$bulan_options['4'] = 'April';
		$bulan_options['5'] = 'Mei';
		$bulan_options['6'] = 'Juni';
		$bulan_options['7'] = 'Juli';
		$bulan_options['8'] = 'Agustus';
		$bulan_options['9'] = 'September';
		$bulan_options['10'] = 'Oktober';
		$bulan_options['11'] = 'November';
		$bulan_options['12'] = 'Desember';

		$divisi_options['all'] = '-- Semua Divisi --';
		foreach ($this->helpdesk_model->get_all_divisi() as $div) {
			$divisi_options[$div->KODE_UNIT_KERJA] = $div->NAMA_UNIT_KERJA;	
		}

		$data['jenis_laporan_options'] = $jenis_laporan_options;
		$data['bulan_options'] = $bulan_options;			
		$data['divisi_options'] = $divisi_options;
		
		$data['content'] = $this->load->view('menu_rekap',$data,true);
		$this->load->view('main',$data);
	
	}
	
	function data()
	{	
		$tahun = $this->input->post('tahun');
		$jenis_laporan = $this->input->post('jenis_laporan');
		$bulan = $this->input->post('bulan');
		$divisi = $this->input->post('divisi');
		
		if ($this->cek_validasi() == FALSE){
			$jenis_laporan_options['all']= '-- Pilih Tipe Rekap --';
			$jenis_laporan_options['0']= 'Excel';
			$jenis_laporan_options['1']= 'Grafik';

			$bulan_options['all'] = '-- Semua Bulan --';
			$bulan_options['1'] = 'Januari';
			$bulan_options['2'] = 'Februari';
			$bulan_options['3'] = 'Maret';
			$bulan_options['4'] = 'April';
			$bulan_options['5'] = 'Mei';
			$bulan_options['6'] = 'Juni';
			$bulan_options['7'] = 'Juli';
			$bulan_options['8'] = 'Agustus';
			$bulan_options['9'] = 'September';
			$bulan_options['10'] = 'Oktober';
			$bulan_options['11'] = 'November';
			$bulan_options['12'] = 'Desember';

			$divisi_options['all'] = '-- Semua Divisi --';
			foreach ($this->helpdesk_model->get_all_divisi() as $div) {
				$divisi_options[$div->KODE_UNIT_KERJA] = $div->NAMA_UNIT_KERJA;	
			}

			$data['jenis_laporan_options'] = $jenis_laporan_options;
			$data['bulan_options'] = $bulan_options;		
			$data['divisi_options'] = $divisi_options;				
			$data['content'] = $this->load->view('menu_rekap',$data,true);
			$this->load->view('main',$data);
		}
		else
		{
			if($jenis_laporan == 0)
			{
				if ($divisi != "all") {
					if ($bulan != "all" ) {
						if ($bulan < 10){
							$bulantahun = '0'.$bulan.$tahun;
						}
						else {
							$bulantahun = $bulan.$tahun;
						}
						
						$helpdesk = $this->helpdesk_model->get_helpdesk_divisi_bulan_tahun($divisi,$bulantahun);

						$data['helpdesk'] = $helpdesk;
						$data['tahun'] = $tahun;
						$data['bulan'] = $this->_baca_bulan($bulan);
						$header = '';
						header("Content-type: application/x-msdownload"); 
				        header("Content-Disposition: attachment; filename=Rekap_helpdesk_".$divisi."_".$this->_baca_bulan($bulan)."_".$tahun.".xls"); 
				        header("Pragma: no-cache"); 
				        header("Expires: 0");
				        echo $header;
						
						$this->load->view('helpdesk/cetak_helpdesk_bulan_tahun',$data);
					} else {
						$helpdesk = $this->helpdesk_model->get_helpdesk_divisi_tahun($divisi,$tahun);

						$data['helpdesk'] = $helpdesk;
						$data['tahun'] = $tahun;
						$header = '';
						header("Content-type: application/x-msdownload"); 
				        header("Content-Disposition: attachment; filename=Rekap_helpdesk_".$divisi."_".$tahun.".xls"); 
				        header("Pragma: no-cache"); 
				        header("Expires: 0");
				        echo $header;
						
						$this->load->view('helpdesk/cetak_helpdesk_tahun',$data);
					}
				} else {
					if ($bulan != "all" ) {
						$helpdesk = $this->helpdesk_model->get_helpdesk_bulan_tahun($bulan,$tahun);

						$data['helpdesk'] = $helpdesk;
						$data['tahun'] = $tahun;
						$data['bulan'] = $this->_baca_bulan($bulan);
						$header = '';
						header("Content-type: application/x-msdownload"); 
				        header("Content-Disposition: attachment; filename=Rekap_helpdesk_".$this->_baca_bulan($bulan)."_".$tahun.".xls"); 
				        header("Pragma: no-cache"); 
				        header("Expires: 0");
				        echo $header;
						
						$this->load->view('helpdesk/cetak_helpdesk_bulan_tahun',$data);
					} else {
						$helpdesk = $this->helpdesk_model->get_helpdesk_tahun($tahun);

						$data['helpdesk'] = $helpdesk;
						$data['tahun'] = $tahun;
						$header = '';
						header("Content-type: application/x-msdownload"); 
				        header("Content-Disposition: attachment; filename=Rekap_helpdesk_".$tahun.".xls"); 
				        header("Pragma: no-cache"); 
				        header("Expires: 0");
				        echo $header;
						
						$this->load->view('helpdesk/cetak_helpdesk_tahun',$data);
					}	
				}				
			}
			else
			{
				$bulan = 1;
				while ($bulan <=12) {
				$requested = $this->helpdesk_model->count_requested_helpdesk($bulan,$tahun);
				foreach ($requested -> result() as $row_requested) {
					$request[] = $row_requested->JUMLAH;
				}
				$solved = $this->helpdesk_model->count_solved_helpdesk($bulan,$tahun);
				foreach ($solved -> result() as $row_solved) {
					$solve[] = $row_solved->JUMLAH;
				}
				$bulan++;
				}

				$data['request'] = $request;
				$data['solved'] = $solve;
				$data['tahun'] = $tahun;
				$data['content'] = $this->load->view('view_charts', $data, true);
				$this->load->view('main',$data);
			}	
		
		}
	}

	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('tahun', 'Tahun', 'required|callback_cek_dropdown');
			$this->form_validation->set_rules('jenis_laporan', 'Jenis laporan', 'required|callback_cek_dropdown_jenis_laporan');
			
			// Setting Error Message
			$this->form_validation->set_message('required', '%s harus diisi!');

			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
			
	}

	function cek_dropdown($value){
	$tahun= $this->input->post('tahun');
	$jenis_laporan= $this->input->post('jenis_laporan');

		if($tahun == "all"){
			$this->form_validation->set_message('cek_dropdown', 'Kolom Tahun wajib dipilih!!');
			return FALSE;
		}		
		else{
			return TRUE;
		}
	}

	function cek_dropdown_jenis_laporan($value){
	$tahun= $this->input->post('tahun');
	$jenis_laporan= $this->input->post('jenis_laporan');

		if($tahun != "all" && $jenis_laporan == "all"){
			$this->form_validation->set_message('cek_dropdown_jenis_laporan', 'Tipe Laporan wajib dipilih!!');
			return FALSE;
		}		
		else{
			return TRUE;
		}
	}

	function _baca($n) {
		$str='';
		$this->dasar = array(1 => 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam','tujuh', 'delapan', 'sembilan');
		$this->angka = array(1000000000, 1000000, 1000, 100, 10, 1);
		$this->satuan = array('milyar', 'juta', 'ribu', 'ratus', 'puluh', '');

		$i = 0;
			if($n==0){
			   $str = "nol";
			}
			else{
			  while ($n != 0) {
				  $count = (int)($n/$this->angka[$i]);
			  if ($count >= 10) {
				  $str .= $this->_baca($count). " ".$this->satuan[$i]." ";
			  }else if($count > 0 && $count < 10){
				  $str .= $this->dasar[$count] . " ".$this->satuan[$i]." ";
			  }
			  $n -= $this->angka[$i] * $count;
			  $i++;
			   }
			   $str = preg_replace("/satu puluh (\w+)/i", "\\1 belas", $str);
			   $str = preg_replace("/satu (ribu|ratus|puluh|belas)/i", "se\\1", $str);
			}
		return $str;
	}
	
	function _format_tanggal($tanggal=1,$bulan=1,$tahun=0,$separator=' ')
	{
		$nama_bulan = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
		return $tanggal.$separator.$nama_bulan[$bulan].$separator.$tahun;
	}
	
	function _format_bulan($bulan=1,$tahun=0,$separator=' ')
	{
		$nama_bulan = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
		return $nama_bulan[$bulan].$separator.$tahun;
	}

	function _baca_bulan($bulan)
	{
		$nama_bulan = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
		return $nama_bulan[$bulan];
	}

}//end of class